package com.kurancy.service.kurancycreated;

import javax.persistence.TypedQuery;

import org.springframework.stereotype.Service;

import com.kurancy.domain.KurancyCreated;
import com.kurancy.domain.enums.KurancyStatus;
import com.kurancy.domain.exception.InvalidTokenException;
import com.kurancy.domain.exception.NotFoundException;

@Service
public class KurancySearchServiceImp implements KurancySearchService {

	@Override
	public KurancyStatus validateToken(String token) throws NotFoundException, InvalidTokenException {
		if (token != null && token.length() > 0) {
			TypedQuery< KurancyCreated> k = KurancyCreated.findKurancyCreatedsByKurancyTokenEquals(token.trim());
			if (k != null && k.getResultList().size() > 0) {
				KurancyCreated c = k.getResultList().get(0);
				
				return c.getStatus();
			} else {
				throw new NotFoundException();
			}
		} else {
			throw new InvalidTokenException();
		}
	}

	@Override
	public KurancyCreated getKurancyCreatedByToken(String token) throws NotFoundException, InvalidTokenException {
		if (token != null && token.length() > 0) {
			TypedQuery< KurancyCreated> k = KurancyCreated.findKurancyCreatedsByKurancyTokenEquals(token.trim());
			if (k != null && k.getResultList().size() > 0) {
				KurancyCreated c = k.getResultList().get(0);
				
				return c;
			} else {
				throw new NotFoundException();
			}
		} else { 
			throw new InvalidTokenException();
		}
	}
}
